package com.why.whypaydemo.controller;


import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayResponse;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.why.whypaydemo.confing.AlipayConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping(value = "/aliPay")
public class AliPCController {

    @Resource
    AlipayConfig alipayConfig;

    @Resource
    AlipayClient alipayClient;

    /**
     * PC支付
     */
    @GetMapping(value = "/pcPay")
    public String pcPay() {
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
//        AlipayTradePrecreateRequest alipayRequest = new AlipayTradePrecreateRequest();

        alipayRequest.setReturnUrl(alipayConfig.getReturnUrl());
        alipayRequest.setNotifyUrl(alipayConfig.getNotifyUrl());

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = "1111119";
        //付款金额，必填
        String total_amount = "1.00";
        //订单名称，必填
        String subject = "lingmeimei";
        //商品描述，可空
        String body = "乖得很9";
        //商品描述，可空
        String qr_pay_mode = "3";

        Number qrcode_width =4;

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"qr_pay_mode\":\"" + qr_pay_mode + "\","
                + "\"qrcode_width\":\"" + qrcode_width + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
        //alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
        //		+ "\"total_amount\":\""+ total_amount +"\","
        //		+ "\"subject\":\""+ subject +"\","
        //		+ "\"body\":\""+ body +"\","
        //		+ "\"timeout_express\":\"10m\","
        //		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
//            AlipayTradePrecreateResponse response = alipayClient.execute(alipayRequest);
//            String bod = response.getBody();
//            JSONObject jsonObject = JSONObject.parseObject(bod);
//             result = jsonObject.getJSONObject("alipay_trade_precreate_response").getString("qr_code");

        } catch (AlipayApiException e) {
            System.out.println("支付出现故障");
            e.printStackTrace();
        }
        System.out.println();
        return result;
    }

    /**
     * 交易查询
     */
    @RequestMapping(value = "/pcPayQuery")
    @ResponseBody
    public void pcPayQuery() {
        //设置请求参数
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

        //商户订单号，商户网站订单系统中唯一订单号
        String out_trade_no = "111111";
        //支付宝交易号
        String trade_no = "1111111111";
        //请二选一设置

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"trade_no\":\"" + trade_no + "\"}");

        //请求
        String result = null;
        try {
            result = alipayClient.execute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            //输出
            System.out.println(result);
            e.printStackTrace();
        }


    }

    /**
     * 退款查询
     */
    @RequestMapping(value = "/refundQuery")
    @ResponseBody
    public void refundQuery() {
        //设置请求参数
        AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();

        //商户订单号，商户网站订单系统中唯一订单号
        String out_trade_no = "11111";
        //支付宝交易号
        String trade_no = "1111";
        //请二选一设置
        //请求退款接口时，传入的退款请求号，如果在退款请求时未传入，则该值为创建交易时的外部交易号，必填
        String out_request_no = "11111";

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"trade_no\":\"" + trade_no + "\","
                + "\"out_request_no\":\"" + out_request_no + "\"}");

        //请求
        try {
            String result = alipayClient.execute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }


    }

    /**
     * 交易关闭
     */
    @RequestMapping(value = "/close")
    @ResponseBody
    public void close() {
        //设置请求参数
        AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();

        //商户订单号，商户网站订单系统中唯一订单号
        String out_trade_no = "11111";
        //支付宝交易号
        String trade_no = "1111";
        //请二选一设置

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"trade_no\":\"" + trade_no + "\"}");

        //请求
        try {
            String result = alipayClient.execute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }


    }
}